package bookpos.action;

import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import bookpos.entity.Book;
import bookpos.entity.Inventory;
import bookpos.service.InventoryService;
import bookpos.util.core.action.BaseAction;
import bookpos.util.core.action.Json;
import bookpos.util.core.util.ExceptionUtil;

import com.opensymphony.xwork2.ModelDriven;
/**
 * 库存控制器 InventoryAction
 * @author WZB
 *
 */
@Controller
@Action(value = "inventory", results = { @Result(name = "inventory", location = "/page/inventory/inventory.jsp") })
public class InventoryAction extends BaseAction implements ModelDriven<Book> {
	private static final long serialVersionUID = 3088843221530537631L;
	private static final Logger logger = Logger.getLogger(InventoryAction.class);
	@Autowired
	private InventoryService service;
	public Book vo = new Book();
	private int bookid;
	private int qt;
	private String returnGoods;//以 ","分割商品id
	private String returnQuantitys;//以 ","分割商品数量
	
	/**
	 * 得到数据列表
	 */
	public void grid(){
		int total = service.getTotal();
		List<Book> list = service.findAll(page, rows, sort, order);		
		writeJson(total,list);
	}
	/**
	 * 增加库存操作
	 */
	public void add(){
			Json result = new Json();
			try {
				System.out.println(qt);
				service.addInventory(bookid, qt);
				result.setSuccess(true);
				result.setMsg("增加库存成功");					
			} catch (Exception e) {
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("增加库存失败");					
			}
			System.out.println(result);
			writeJson(result);
	}
	/**
	 * 减少库存操作
	 */
	public void sub() {
			Json result = new Json();
			try {
				service.subInventory(getBookid(), getQt());
				result.setSuccess(true);
				result.setMsg("减少库存成功");
				result.setSuccess(true);
			} catch (Exception e) {
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("减少库存失败");
			}
			writeJson(result);
	}
	/**
	 * 退货方法
	 */
	public void returnGoods() {
			Json result = new Json();
			try {
				String[] ids=getReturnGoods().split(",");
				String[] qs=getReturnQuantitys().split(",");
				service.returnGoods(ids, qs);
				result.setSuccess(true);
				result.setMsg("增加库存成功");
				result.setSuccess(true);
			} catch (Exception e) {
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("减少库存失败");
			}
			writeJson(result);
	}
	/**
	 * 销售库存操作方法
	 */
//	public void saleGoods() {
//			Json result = new Json();
//			try {
//				String[] ids=getReturnGoods().split(",");
//				String[] qs=getReturnQuantitys().split(",");
//				service.saleGoods(ids, qs);
//				result.setSuccess(true);
//				result.setMsg("减少库存成功");
//				result.setSuccess(true);
//			} catch (Exception e) {
//				logger.debug(ExceptionUtil.getExceptionMessage(e));
//				result.setMsg("减少库存失败");
//			}
//			writeJson(result);
//	}
	/**
	 * 返回商品json方法
	 */
	public void getGoodByID() {
		Json result = new Json();
		try {
			Book book = service.findById(getBookid());
			if(book!=null){
				result.setSuccess(true);
				result.setMsg("success");
				result.setObj(book);
				writeJson(book);
			}
			
		} catch (Exception e) {
			logger.debug(ExceptionUtil.getExceptionMessage(e));
			result.setMsg("减少库存失败");
		}
		
		
	}
	
	public Book getModel() {
		return vo;
	}
	public int getBookid() {
		return bookid;
	}
	public void setBookid(int bookid) {
		this.bookid = bookid;
	}
	public int getQt() {
		return qt;
	}
	public void setQt(int qt) {
		this.qt = qt;
	}
	public String getReturnQuantitys() {
		return returnQuantitys;
	}
	public void setReturnQuantitys(String returnQuantitys) {
		this.returnQuantitys = returnQuantitys;
	}
	public String getReturnGoods() {
		return returnGoods;
	}
	public void setReturnGoods(String returnGoods) {
		this.returnGoods = returnGoods;
	}	
	
}
